package com.xx.sg.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xx.sg.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/xg")
@Tag(name = "徐工设备", description = "徐工设备API")
public class XuGongDevController {

    @Value("${transfer.host}")
    private String XG_HOST;
    @Value("${transfer.xg.prefix}")
    private String XG_PREFIX;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    @Operation(summary = "查看单个", description = "查看单个")
    public Map devInfo(@PathVariable String id) {

        String url = XG_HOST + XG_PREFIX + "/business/dbdVehicle/open/working/" + id;

        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + getToken());
        // 构建请求实体（GET 没有请求体，这里只传 headers）
        HttpEntity<String> entity = new HttpEntity<>(headers);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "成功");
        res.put("data", jsonObject);
        return res;
    }

    @PostMapping("/list")
    @Operation(summary = "批量查询", description = "批量查询多个id用逗号分隔")
    public R workList(@RequestParam String vincodes) {
        String url = XG_HOST + XG_PREFIX + "/business/dbdVehicle/open/workingList";

        // 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + getToken());
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 携带的参数：form-data 形式
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("vincodes", vincodes);

        // 构建请求实体（GET 没有请求体，这里只传 headers）
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);

        // 发起 POST 请求
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        return R.success(jsonObject);
    }


    private String getToken() {

        String url = XG_HOST + XG_PREFIX + "/auth/oauth/token?login_type=2";

        // 封装表单参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("username", "huricha");
        params.add("password", "hrc111111");
        params.add("scope", "server");
        params.add("grant_type", "password");

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 设置为 JSON
        headers.add("Authorization", "Basic cGlnOnBpZw==");

        // 构造请求体
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);

        // 发送 POST 请求
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        String token = jsonObject.getString("access_token");
        return token;
    }

}
